package com.xbfinal.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbfinal.commonUtils.JwtUtils;
import com.xbfinal.commonUtils.Result;
import com.xbfinal.commonUtils.ordervo.CourseWebVoOrder;
import com.xbfinal.eduservice.client.OrderClient;
import com.xbfinal.eduservice.entity.EduCourse;
import com.xbfinal.eduservice.entity.chapter.ChapterVo;
import com.xbfinal.eduservice.entity.frontvo.CourseFrontVo;
import com.xbfinal.eduservice.entity.frontvo.CourseWebVo;
import com.xbfinal.eduservice.service.EduChapterService;
import com.xbfinal.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Title: CourseFrontController
 * @Author 笑霸fianl
 * @Package com.xbfinal.eduservice.controller.front
 * @Date 2023/3/5 16:18
 * @描述:
 */
@RestController
@RequestMapping("/eduservice/coursefront")
@Api(description = "前台课程")
@Slf4j
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private  OrderClient orderClient;


    @ApiOperation("带条件的课程分页查询")
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public Result getFrontCourseList(@PathVariable long page,
                                     @PathVariable long limit,
                                     @RequestBody(required = false) CourseFrontVo courseFrontVo) {

        log.info(courseFrontVo.toString());
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);

        log.info(map.toString());
        //返回分页所有数据
        return Result.ok().addData(map);
    }

    @ApiParam(defaultValue = "课程详情方法")
    @GetMapping("getFrontCourseInfo/{courseId}")
    public Result getFrontCourseInfo(@PathVariable("courseId") String courseId, HttpServletRequest request){

        //根据id查看课程信息
         CourseWebVo courseWebVo= courseService.getBaseCourseInfo(courseId);

         //根据id 查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        //根据课程id和用户id查询课程是否已经购买过了
        String id = JwtUtils.getMemberIdByJwtToken(request);
        boolean budCourse = orderClient.isBudCourse(courseId,id);
        return Result.ok().add("courseWebVo",courseWebVo).add("chapterVideoList",chapterVideoList).add("isBuy",budCourse);
    }


    @ApiParam(defaultValue = "根据课堂id获取课堂信息")
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable("id") String id){

        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder voOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(baseCourseInfo,voOrder);
        return voOrder;
    }


}
